<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>添加部门</title>
        <script src="/jquery/jquery-3.2.1.min.js"></script>
        <script src="/layui/layui.js"></script>
        <link rel="stylesheet" href="/layui/css/layui.css">
        <style>
            .box{
                padding: 20px;
            }
            #deptDescirbe{
                resize:none;
                width: 440px
            }
            form{
                position: relative;
            }
            .buttonBox{
                position: absolute;
                right: 0;
                bottom: -45px;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门名称:</label>
                        <div class="layui-input-inline">
                            <input name="deptName" id="deptName" placeholder="请输入部门名称" class="layui-input"  lay-verify="required" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门描述:</label>
                        <div class="layui-input-block">
                            <textarea id="deptDescirbe" name="deptDescirbe" placeholder="请输入部门描述" required lay-verify="required" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门权限:</label>
                        <div class="layui-input-block">
                            <div id="checkboxBox">
                                <input type="checkbox" value="1" name="authorityId" title="管理员" lay-skin="primary" >
                                <input type="checkbox" value="2" name="authorityId" title="教师" lay-skin="primary">
                                <input type="checkbox" value="3" name="authorityId" title="运营" lay-skin="primary" >
                                <input type="checkbox" value="4" name="authorityId" title="人事" lay-skin="primary" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="buttonBox">
                    <button class="layui-btn layui-btn-normal  layui-btn-sm" lay-filter="sub" lay-submit type="submit">添加</button>
                    <button class="layui-btn layui-btn-primary  layui-btn-sm" type="button" id="close">取消</button>
                </div>
                <input type="hidden" name="parentId" value="0">
                <input type="hidden" name="status" value="1">
                <input type="hidden" name="deptIden" value="暂无">
                <input type="hidden" name="createId" id="createId">
            </form>
        </div>
        <script>
            layui.use(['jquery','form'],function () {
                var $ = layui.$;
                var form = layui.form;
                //关闭按钮
                $('#close').click(function () {
                    var layer = parent.layer;
                    layer.close(layer.index);
                });
                var user = JSON.parse(localStorage.getItem('user'));
                //创建人赋值
                $('#createId').val(user.data.userId);
                //提交按钮
                form.on('submit(sub)',function (obj) {
                    console.log(obj.field);
                    //添加部门
                    $.ajax({
                        url: '/login/addDept',
                        data: obj.field,
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            if(data.data.id != null){
                                //获取选中的复选框
                                var authorityIds = new Array();
                                var authorityId = $("input[name='authorityId']");
                                for (var i = 0; i < authorityId.length; i++) {
                                    if(authorityId[i].checked){
                                        authorityIds.push(authorityId[i].value);
                                    }
                                }
                                if(authorityIds.length > 0){
                                    //添加部门权限
                                    $.ajax({
                                        url: '/jsDeptAuthority/addDeptAuthoritys',
                                        type: 'post',
                                        dataType: 'json',
                                        data: {
                                            type: 2,
                                            deptId: data.data.id,
                                            trIds: authorityIds
                                        },
                                        success: function (data) {
                                            if(data.data){
                                                var layer = parent.layer;
                                                layer.close(layer.index);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                    return false;
                });
            });
        </script>
    </body>
</html>